<?php

/**
 * 分销商二维码
 */

namespace app\sale\common;

class QrcodeCommon extends \app\sale\common\SaleCommon {


    protected function index() {
        $this->pageInfo['name'] = '我的二维码';
        $this->setMeta('我的二维码');
        $this->setCrumb([
            [
                'name' => '我的二维码',
                'url' => url()
            ]
        ]);

        $path = 'upload/qrcode/' . $this->userInfo['user_id'] . '/';
        $dir = ROOT_PATH . $path;
        if (!is_dir($dir)) {
            if (!\mkdir($dir, 0777, true)) {
                $this->error('upload目录没有写入权限！');
            }
        }

        $shareFile = $dir . 'share.jpg';
        $shareUrl = DOMAIN . url('member/login/index', ['sale_code' => $this->saleInfo['code']]);

        if (!is_file($shareFile)) {
            $sharePath = ROOT_PATH . 'public/sale/images/share-user.jpg';
            $qrcodePath = $dir . "qrcode.png";
            \PHPQRCode\QRcode::png($shareUrl, $qrcodePath, 'L', 11, 1);

            //缩放二维码
            $dstCode = imagecreatefromstring(file_get_contents($qrcodePath));
            $qrcodeInfo = getimagesize($qrcodePath);
            $thumb = imagecreatetruecolor(300, 300);
            imagecopyresampled($thumb, $dstCode, 0, 0, 0, 0, 300, 300, $qrcodeInfo[0], $qrcodeInfo[1]);

            //写入二维码
            $dstShare = @imagecreatefromstring(file_get_contents($sharePath));
            imagecopy($dstShare, $thumb, 80, 491, 0, 0, 300, 300);
            imagedestroy($dstCode);
            imagedestroy($thumb);

            //写入头像
            if(!empty($this->userInfo['avatar'])) {
                $dstAvatar = imagecreatefromstring(file_get_contents(ROOT_PATH . $this->userInfo['avatar']));
                if ($dstAvatar) {
                    imagecopy($dstShare, $dstAvatar, 42, 50, 0, 0, 128, 128);
                }
                imagedestroy($dstAvatar);
            }

            //写入用户名
            $font = ROOT_PATH . 'public/common/fonts/adobehei.otf';
            $red = imagecolorallocate($dstShare, 255, 0, 0);
            imagefttext($dstShare, 26, 0, 266, 100, $red, $font, $this->userInfo['show_name']);

            //写入网站标题
            $gray = imagecolorallocate($dstShare, 169, 169, 169);
            imagefttext($dstShare, 26, 0, 195, 155, $gray, $font, '我为' . $this->siteConfig['info_title'] . '代言');

            //写入网站名称
            $white = imagecolorallocate($dstShare, 255, 255, 255);
            $text = '扫一扫，进入' . $this->siteConfig['info_name'] . '商城';
            $len = mb_strlen($text, "UTF-8");
            $left = (720 - $len * 32) / 2;
            imagefttext($dstShare, 30, 0, $left, 375, $white, $font, $text);


            ob_start();//启用输出缓存，暂时将要输出的内容缓存起来
            imagejpeg($dstShare, NULL, 100);//输出
            $poster = ob_get_contents();//获取刚才获取的缓存
            ob_end_clean();//清空缓存
            imagedestroy($dstShare);

            file_put_contents($shareFile, $poster);
        }

        $shareImg = ROOT_URL . '/' . $path . 'share.jpg';

        $this->assign('share_img', $shareImg);
        $this->assign('share_url', $shareUrl);
        $this->assign('sale_code', $this->saleInfo['code']);
        $this->memberDisplay();
    }


}